﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
using BUS;
using System.Collections;
using System.Text;
using System.Security.Cryptography;

namespace FastDeal.Control
{
    public partial class wuc_ThongTinTaiKhoan : System.Web.UI.UserControl
    {
       
        private TaiKhoan taikhoan= new TaiKhoan();
        private ThongTinTaiKhoan tttaikhoan = new ThongTinTaiKhoan() ;
        protected void Page_Load(object sender, EventArgs e)
        {
            // cái này là gán sẵn, trước khi gọi form này thì gán username đã đăng nhập
            taikhoan.UserID = int.Parse(Session["UserID"].ToString()); ;
            tttaikhoan.UserID = taikhoan.UserID;

            if (!IsPostBack)
            {               
                LoadThongTinTaiKhoan();
            }
        }
        TaiKhoan tk = new TaiKhoan();
        ThongTinTaiKhoan tttk = new ThongTinTaiKhoan();
        TaiKhoan_BUS tkBUS = new TaiKhoan_BUS();
        ThongTinTaiKhoan_BUS tttkBUS = new ThongTinTaiKhoan_BUS();
        public void LoadThongTinTaiKhoan()
        {
          
            // lấy thông tin tài khoản
            tk = tkBUS.LayThongTin_TaiKhoan(taikhoan.UserID);
            tttk = tttkBUS.LayThongTin_ThongTinTaiKhoan(tttaikhoan.UserID);

            txtUserName.Text = tk.UserName;
            txtEmail.Text = tk.Email;
            txtHoTen.Text = tttk.HoTen;
            txtNickName.Text = tttk.Nickname;
            txtDiaChi.Text = tttk.DiaChi;
            txtDienThoai.Text = tttk.DienThoai;
            txtCMND.Text = tttk.CMND;
        }
        public string MaHoaMatKhau(string password)
        {
            UnicodeEncoding encoding = new UnicodeEncoding();
            Byte[] hashBytes = encoding.GetBytes(password);
            // Compute the SHA-1 hash
            SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
            Byte[] cryptPassword = sha1.ComputeHash(hashBytes);
            return BitConverter.ToString(cryptPassword).Substring(0, 8);
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (txtPassword.Text == String.Empty)
            {
                ThongTinTaiKhoan tttkDTO = new ThongTinTaiKhoan();
                tttkDTO.UserID = taikhoan.UserID;
                tttkDTO.HoTen = txtHoTen.Text;
                tttkDTO.Nickname = txtNickName.Text;
                tttkDTO.DiaChi = txtDiaChi.Text;
                tttkDTO.DienThoai = txtDienThoai.Text;
                tttkDTO.CMND = txtCMND.Text;
                tttkBUS.Sua_ThongTinTaiKhoanChiTiet(tttkDTO);
                LoadThongTinTaiKhoan();
            }
            else    
                if (txtPassword.Text == txtConfirmPassword.Text)
	            {
		            TaiKhoan tkDTO = new TaiKhoan();
                    tkDTO.UserID = taikhoan.UserID;
                    tkDTO.Password =  MaHoaMatKhau(txtPassword.Text);
                    tkBUS.Sua_TaiKhoanDoiPass(tkDTO);
                    ThongTinTaiKhoan tttkDTO = new ThongTinTaiKhoan();
                    tttkDTO.UserID = taikhoan.UserID;
                    tttkDTO.HoTen = txtHoTen.Text;
                    tttkDTO.Nickname = txtNickName.Text;
                    tttkDTO.DiaChi = txtDiaChi.Text;
                    tttkDTO.DienThoai = txtDienThoai.Text;
                    tttkDTO.CMND = txtCMND.Text;
                    tttkBUS.Sua_ThongTinTaiKhoanChiTiet(tttkDTO);
                    LoadThongTinTaiKhoan();
	            }
        }

     
    }
}